﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Reflection;
using System.ComponentModel;

namespace Gtkj.CMS.Common
{
    public class EnumToList
    {

        /// <summary>
        /// 将enum转换成List<Model.Models.SelectViewModels>
        /// </summary>
        /// <returns></returns>
        public static List<Model.Models.SelectViewModels> EnumToSelect(Type t)
        {
            List<Model.Models.SelectViewModels> select = new List<Model.Models.SelectViewModels>();
            foreach (var e in Enum.GetValues(t))
            {
                Model.Models.SelectViewModels model = new Model.Models.SelectViewModels();
                model.Text = e.ToString();
                model.Value = Enum.Format(t, e, "d");
                select.Add(model);
            }
            return select;
        }



        /// <summary>
        /// 获取枚举描述信息
        /// </summary>
        /// <param name="enumType">枚举类型</param>
        /// <param name="enumStr">枚举字值字符串</param>
        /// <returns></returns>
        public static string GetEnumDescription(Type enumType, string enumStr)
        {
            FieldInfo p = enumType.GetField(enumStr);
            object[] os = p.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), true);
            if (os == null || os.Length == 0)
            {
                return null;
            }
            return ((DescriptionAttribute)os[0]).Description;

        }


    }
}
